<?php
class Controller_Getcontent extends Controller_Template
{
	protected $template = 'download';
	public function actionDownload($token=false)
	{
            if ($token ? $token != '' : false) {
                $cart = Model::factory('cart');
                $directory = Model::factory('directory');
                $order = $cart->getOrderByToken($token);
                if (isset($order) && $order['isPaid'] == '1') {
                    if ($order['delivery'] == 'Direct Download') {
                        $items = $cart->getOrderItems($order['id']);
                        if (count($items) > 1) {
                            $file_names = array();
                            foreach ($items as $item) {
                                if ($item['section'] != null) {
                                    $attachment = $directory->getAttachment($item['attachmentId'],$item['section']);
                                    $file_names[] = array('location' => $directory->attachmentPath1($attachment), 'name' => $attachment['name']);
                                } else {
                                    $file_names[] = array('location' => '/content/custom/'.$order['sessionId'].'/test.pdf', 'name' => 'Custom sign.');
                                }
                            }
                            $zip_name = array('location'=>'/content/download/'.$token.'.zip','name'=>$token.'.zip');
                            $this->zipFilesAndDownload($file_names,$zip_name,'/');
                        } elseif (count($items) == 1)  {
                            $items = $cart->getOrderItems($order['id']);
                            foreach ($items as $item) {
                                if ($item['section'] != null) {
                                    $attachment = $directory->getAttachment($item['attachmentId'],$item['section']);
                                    $file_location = $directory->attachmentPath1($attachment);
                                    $file_name = $token.'.pdf';
                                } else {
                                    $file_location = '/content/custom/'.$order['sessionId'].'/test.pdf';
                                    $file_name = 'Custom_sign.pdf';
                               }
                            }
//                            echo $file_location.'<br>';
//                            echo $file_name;exit;
                            header('Content-type: application/pdf');
                            header("Content-Disposition: attachment; filename=$file_name");
                            header("Pragma: no-cache");
                            header("Expires: 0");
                            readfile($file_location);
                            exit;
                        }
                    } else {
                        $this->template->text = 'You select not. Direct Download delivery.';
                        return;
                    }
                } else {
                    $this->template->text = 'Wrong order item. Or not yet payed.';
                    return;
                }
            } else {
                $this->template->text = 'Wrong link to file download.';
                return;
            }
            
	}
        
        function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
        {
            $zip = new ZipArchive();
            //create the file and throw the error if unsuccessful
            if ($zip->open($archive_file_name['location'], ZIPARCHIVE::CREATE )!==TRUE) {
//                exit("cannot open <$archive_file_name[]>\n");
            }
            //add each files of $file_name array to archive
            foreach($file_names as $files)
            {
                
                $zip->addFile($file_path.$files['location'],$files['name'].'.pdf');
//                echo $file_path.$files,$files."<br>";
            }
            $zip->close();
            $name = $archive_file_name['name'];
            //then send the headers to foce download the zip file
            header("Content-type: application/zip");
            header("Content-Disposition: attachment; filename=$name");
            header("Pragma: no-cache");
            header("Expires: 0");
            readfile($archive_file_name['location']);
            exit;
    }
}
